﻿using RpcHelper;

using RpcManageClient;

using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class RemoteGroupCollect : BasicCollect<DAL.RemoteServerGroupDAL>, IRemoteGroupCollect
        {
                private IRpcServerCollect _RpcServer => this.GetCollect<IRpcServerCollect> ();
                public RemoteGroup[] GetServers (long merId)
                {
                        return this.BasicDAL.GetServers (merId);
                }
                public bool CheckIsExists (long merId)
                {
                        return this.BasicDAL.CheckIsExists (merId);
                }
                private void _CheckIsExists (long merId, long serverId)
                {
                        if (this.BasicDAL.CheckIsExists (merId, serverId))
                        {
                                throw new ErrorException ("rpc.remote.group.already.bind");
                        }
                }
                public void DropBind (long id)
                {
                        RemoteGroupAddParam data = this._Get (id);
                        this.BasicDAL.DropBind (id);
                        this._RpcServer.RefreshConfig (data.RpcMerId, data.SystemType);
                }
                private RemoteGroupAddParam _Get (long id)
                {
                        RemoteGroupAddParam param = this.BasicDAL.Get (id);
                        if (param == null)
                        {
                                throw new ErrorException ("rpc.remote.group.not.find");
                        }
                        return param;
                }
                public void SetBindGroup (long merId, ServerConfigDatum server)
                {
                        this._CheckIsExists (merId, server.Id);
                        ServerType type = this.GetCollect<IServerTypeCollect> ().Get (server.SystemType);
                        RemoteGroupAddParam add = new RemoteGroupAddParam
                        {
                                RpcMerId = merId,
                                ServerId = server.Id,
                                RegionId = server.RegionId,
                                SystemType = server.SystemType,
                                TypeVal = type.TypeVal
                        };
                        this.BasicDAL.Add (add);
                        this._RpcServer.RefreshConfig (merId, type.Id);
                }
                public void SetWeight (long id, int weight)
                {
                        this.BasicDAL.SetWeight (id, weight);
                }
                public RemoteGroup[] GetServers (long rpcMerId, long[] serverId)
                {
                        return this.BasicDAL.GetServers (rpcMerId, serverId);
                }
        }
}
